package com.kucai.yifaqapp.ui.fragment.homePageFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResBanner;
import com.kucai.yifaqapp.model.network.entity.res.ResKaiyuanGame;
import com.kucai.yifaqapp.ui.activity.BaseActivity;
import com.kucai.yifaqapp.ui.activity.WebActivityGame;
import com.kucai.yifaqapp.ui.fragment.LauncherViewPagerHomeFragment;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.widget.AlertDialogIos;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/11/10.
 */
public class QipaiFragment extends Fragment {
    private RecyclerView qipailist;
    private ResBanner.DataBeanX.GamesBean mgamedatas;
    private String url;
    private int flag;//右侧游戏加载标记 1==开元  2==龙城  3==无双 4==vg
    Adapter2 adapter2;
    Adapter1 adapter1;
    private RecyclerView game_list_content;
    //开元棋牌游戏字库
    String[] kaiyuantv = {"德州扑克", "二八杠", "抢庄牛牛", "炸金花", "三公", "押庄龙虎", "21点", "通比牛牛", "急速炸金花",
            "抢庄牌九", "十三水", "百家乐", "幸运五张", "斗地主", "射龙门"};

    //开元棋牌URLTAG
    String[] kaiyuantag = {"dzpk", "ebg", "qznn", "zjh", "sg", "yzlh", "21d", "tbnn", "jszjh",
            "qzpj", "sss", "bjl", "xywz", "ddz", "slm"};
    //开元棋牌游戏图片
    int[] kaiyuanimg = {R.mipmap.dezhou, R.mipmap.erbagang, R.mipmap.qiangzhuang, R.mipmap.zhajinhua, R.mipmap.sangong,
            R.mipmap.yazhuanglonghu, R.mipmap.ershiyi, R.mipmap.tongbiniuniu, R.mipmap.jisuzhajinhua, R.mipmap.qiangzhuangpaijiu
            , R.mipmap.shisanshui, R.mipmap.baijiale, R.mipmap.xingyunwuzhang, R.mipmap.doudizhu, R.mipmap.shelongmen};

    //龙城棋牌游戏字库
    String[] longchengtv = {"抢庄牛牛", "龙虎斗", "二八杠", "百家乐", "通比牛牛", "三公", "炸金花", "斗地主", "牌九"};
    String[] longchengurl = {"qznn", "lhd", "ebg", "bjl", "tbnn", "sg", "zjh", "ddz", "pj"};
    //龙城棋牌游戏图片
    int[] longchengimg = {R.mipmap.lcqiangzhuangniuniu, R.mipmap.lclonghudou, R.mipmap.lcerbagang, R.mipmap.lcbaijiale, R.mipmap.lctongbiniuniu,
            R.mipmap.lcsangong, R.mipmap.lczhajinhua, R.mipmap.lcdoudizhu, R.mipmap.lcpaijiu};

    //龙城棋牌游戏字库
    String[] wushuangtv = {"百家乐", "无双捕鱼", "无双港五", "金皇港", "牛牛", "时时彩梭哈", "时时彩牛牛", "通比牛牛", "炸金花"};
    //龙城棋牌游戏图片
    int[] wushuangimg = {R.mipmap.wsbaijiale, R.mipmap.wsbuyu, R.mipmap.wsgangwu, R.mipmap.wsjinhuangguan, R.mipmap.wsniuniu,
            R.mipmap.wssuoha, R.mipmap.wssscniuniu, R.mipmap.wstbniuniu, R.mipmap.wszhajinhua};
    //VG棋牌字库
    String[] vgtv = {"推筒子", "十三水", "百人牛牛", "抢庄牛牛", "多财多福", "斗地主", "保险楚汉德州", "炸金花", "竞咪楚汉德州", "血战麻将", "加倍斗地主", "必下德州", "百人三公", "龙王捕鱼"};

    int[] vgiv = {R.mipmap.vgtuitongzi, R.mipmap.vgshisanshui, R.mipmap.vgniuniu, R.mipmap.vgqiangzhuang, R.mipmap.vgduocaiduofu, R.mipmap.vgdoudizhu,
            R.mipmap.vgbaoxian, R.mipmap.vgzhajinhua, R.mipmap.vgjingmi, R.mipmap.vgxuezhan, R.mipmap.vgjiabeidoudizhu, R.mipmap.bixiadezhou, R.mipmap.vgbairensangong, R.mipmap.vg_longwang
    };
    String[] vgtag = {"8", "15", "4", "3", "6", "1", "10", "12", "7", "11", "9", "13", "14", "5"};
    private AlertDialogIos myDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.qipai_fragment, container, false);
        qipailist = view.findViewById(R.id.game_list);
        LinearLayoutManager ms = new LinearLayoutManager(getActivity());
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
        qipailist.setLayoutManager(ms);
        for (int x = 0; x < mgamedatas.data.size(); x++) {
            if (x == 0) {
                mgamedatas.data.get(x).ischoose = true;
                if (mgamedatas.data.get(0).g_id.equals("15")) {
                    flag = 1;
                } else if (mgamedatas.data.get(0).g_id.equals("16")) {
                    flag = 2;
                } else if (mgamedatas.data.get(0).g_id.equals("17")) {
                    flag = 3;
                } else if (mgamedatas.data.get(0).g_id.equals("18")) {
                    flag = 4;
                }
            } else {
                mgamedatas.data.get(x).ischoose = false;
            }
        }
        adapter2 = new Adapter2();
        qipailist.setAdapter(adapter2);
        game_list_content = view.findViewById(R.id.game_list_content);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        game_list_content.setLayoutManager(gridLayoutManager);
        game_list_content.setHasFixedSize(true);
        adapter1 = new Adapter1();
        game_list_content.setAdapter(adapter1);
        return view;
    }

    public void setData(ResBanner.DataBeanX.GamesBean gamedatas) {
        this.mgamedatas = gamedatas;
        if(adapter2!=null){
            adapter2.notifyDataSetChanged();
        }
        if(adapter1!=null){
            adapter1.notifyDataSetChanged();
        }
    }

    class Adapter2 extends RecyclerView.Adapter<Adapter2.MViewHolder1>
            implements OnItemClickListener {
        @Override
        public Adapter2.MViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter2.MViewHolder1(LayoutInflater.from(getContext())
                    .inflate(R.layout.gameleibieitem, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter2.MViewHolder1 holder, final int position) {
            //开始设置游戏类别按钮和属性

            if (mgamedatas.data.get(position).g_id.equals("15")) {
                holder.gameitem.setText("开元棋牌");
                holder.gameitem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        flag = 1;
                        url = "15";
                        for (int x = 0; x < mgamedatas.data.size(); x++) {
                            if (x == position) {
                                mgamedatas.data.get(x).ischoose = true;
                            } else {
                                mgamedatas.data.get(x).ischoose = false;
                            }
                        }
                        if(adapter2!=null){
                            adapter2.notifyDataSetChanged();
                        }
                        if(adapter1!=null){
                            adapter1.notifyDataSetChanged();
                        }
                    }
                });
            } else if (mgamedatas.data.get(position).g_id.equals("16")) {
                holder.gameitem.setText("龍城游戏");
                holder.gameitem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        flag = 2;
                        url = "16";
                        for (int x = 0; x < mgamedatas.data.size(); x++) {
                            if (x == position) {
                                mgamedatas.data.get(x).ischoose = true;
                            } else {
                                mgamedatas.data.get(x).ischoose = false;
                            }
                        }
                        if(adapter2!=null){
                            adapter2.notifyDataSetChanged();
                        }
                        if(adapter1!=null){
                            adapter1.notifyDataSetChanged();
                        }
                    }
                });
            } else if (mgamedatas.data.get(position).g_id.equals("17")) {
                holder.gameitem.setText("无双棋牌");
                holder.gameitem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        flag = 3;
                        url = "17";
                        for (int x = 0; x < mgamedatas.data.size(); x++) {
                            if (x == position) {
                                mgamedatas.data.get(x).ischoose = true;
                            } else {
                                mgamedatas.data.get(x).ischoose = false;
                            }
                        }
                        if(adapter2!=null){
                            adapter2.notifyDataSetChanged();
                        }
                        if(adapter1!=null){
                            adapter1.notifyDataSetChanged();
                        }
                    }
                });
            } else if (mgamedatas.data.get(position).g_id.equals("18")) {
                holder.gameitem.setText("VG棋牌");
                holder.gameitem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        flag = 4;
                        url = "18";
                        for (int x = 0; x < mgamedatas.data.size(); x++) {
                            if (x == position) {
                                mgamedatas.data.get(x).ischoose = true;
                            } else {
                                mgamedatas.data.get(x).ischoose = false;
                            }
                        }
                        if(adapter2!=null){
                            adapter2.notifyDataSetChanged();
                        }
                        if(adapter1!=null){
                            adapter1.notifyDataSetChanged();
                        }
                    }
                });
            }
            //最后将我们选中的更新
            if (mgamedatas.data.get(position).ischoose) {
                holder.gameitem.setBackgroundResource(R.mipmap.heiseqipai);
                holder.gameitem.setTextColor(getContext().getResources().getColor(R.color.settingbg));
            } else {
                holder.gameitem.setBackgroundResource(R.mipmap.baiseqipai);
                holder.gameitem.setTextColor(getContext().getResources().getColor(R.color.blank));
            }
        }

        @Override
        public int getItemCount() {
            if (mgamedatas != null) {
                if (mgamedatas.data != null) {
                    return mgamedatas.data.size();
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder1 extends RecyclerView.ViewHolder {
            TextView gameitem;

            MViewHolder1(View itemView) {
                super(itemView);
                gameitem = (TextView) itemView.findViewById(R.id.gameitme);
            }
        }

    }


    ////////////////////////////////////////////右侧游戏/////////////////////////////////////////////////////
    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter1.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter1.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.game_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter1.MViewHolder holder, final int position) {
            if (flag == 1) {
                holder.geme_tv.setText(kaiyuantv[position]);
                Glide.with(getContext()).load(kaiyuanimg[position]).into(holder.game_iv);
            }
            if (flag == 2) {
                holder.geme_tv.setText(longchengtv[position]);
                Glide.with(getContext()).load(longchengimg[position]).into(holder.game_iv);
            }
            if (flag == 3) {
                holder.geme_tv.setText(wushuangtv[position]);
                Glide.with(getContext()).load(wushuangimg[position]).into(holder.game_iv);
            }
            if (flag == 4) {
                holder.geme_tv.setText(vgtv[position]);
                Glide.with(getContext()).load(vgiv[position]).into(holder.game_iv);
            }
            /**
             * 	‘token’ =>, //用户token，
             ‘g_id’ => 15， // 接口id  开元 龙成
             ‘tag’ => 1,   //接口游戏id
             ‘doingCode’ => 1， //请求类型 0:登录 1:刷新余额
             */
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Info.user != null) {
                        myDialog = new AlertDialogIos(getActivity()).builder();
                        myDialog.setGone().setMsg("是否进入游戏").setNegativeButton("取消", R.color.skyblue, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialog.dismiss();
                            }
                        }).setPositiveButton("确定", R.color.skyblue, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialog.dismiss();
                                //这里点击进入游戏，待处理
                                if (flag == 1) {
                                    getkaiyuangame(Info.user.token, "15", kaiyuantag[position], "0");
                                } else if (flag == 2) {
                                    getkaiyuangame(Info.user.token, "16", longchengurl[position], "0");
                                } else if (flag == 3) {
                                   getkaiyuangame(Info.user.token, "17", "", "0");
                                } else if (flag == 4) {
                                    getkaiyuangame(Info.user.token, "18", vgtag[position], "0");
                                }
                            }
                        }).show();


                    } else {
                        ((BaseActivity) getActivity()).startLogin();
                    }

                }
            });

        }

        @Override
        public int getItemCount() {
            if (flag == 1) {
                return kaiyuantv.length;
            } else if (flag == 2) {
                return longchengtv.length;
            } else if (flag == 3) {
                return wushuangtv.length;
            } else if (flag == 4) {
                return vgtv.length;
            } else {
                return 0;
            }

        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView geme_tv;
            ImageView game_iv;

            MViewHolder(View itemView) {
                super(itemView);
                geme_tv = (TextView) itemView.findViewById(R.id.geme_tv);
                game_iv = (ImageView) itemView.findViewById(R.id.game_iv);

            }
        }
    }

    //获取游戏url
    public void getkaiyuangame(String token, String urlid, String gid, String tag) {
        HttpJsonUtils.getHttpApiInstance()
                .getkaiyuangame(HttpAdress.getuseAdress() + "api/GamesApi/action", token, urlid, gid, tag)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResKaiyuanGame>() {

                    @Override
                    protected void onCustomNext(ResKaiyuanGame resKaiyuanGame) {
                        if (resKaiyuanGame.code == 0 && resKaiyuanGame.data != null) {
                            Intent intent = new Intent(getContext(), WebActivityGame.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("url", resKaiyuanGame.data.url);
                            bundle.putString("title", "空");
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getContext(), resKaiyuanGame.msg, Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }


}
